<?php

Class FlatFileDB {

	private $baseDir;
	private $ext;
	private $userListCache = Array();

	//Set up db
	public function __construct($options) {
		$this->baseDir 		= $options['directory'];
		$this->ext 			= $options['ext'];
	}
	
	public function get_row($table, $getByColum, $getByValue){

	
	
		if($table !== 'users') die('throw error unsupported operation');
		if($getByColum == 'name'){
		
			$usrList = $this->loadUserList();	
			$getByValue = $usrList[trim($getByValue)];
		}
		$userFile = 'users'.'/'.$getByValue;
		
		//return data
		if($this->fileExist($userFile)){

			$userDataArray = array();
			foreach($this->readAsArray($userFile) AS $userData)
			{
				$value = explode('|',$userData);
				$userDataArray[$value[0]] = trim($value[1]);
			}
			return $userDataArray;
		}else{
			return null;
		}
			//Ether everything will have stopped or we now have a user id, lets load em
			
	}
	
	public function  get_rows($table,$valuesToReturn,$start=0,$limit=50){
	
		if($valuesToReturn!==array('uid','name')) die(' Unsupported operation ');
	
			$usrList = $this->loadUserList();
			
		if($limit+$start !== 0){
			$newUserList = Array();
			$limitCount=0;
			
			foreach($usrList AS $name => $uid ){
				if($id >= $start){
					$newUserList[] = Array('uid'=> $uid, 'name' => $name);
					$limitCount+1;
				}
				if($limitCount >= $limit){ break;}
			}
			$usrList = $newUserList;
		}
		return $usrList;
	}
	
	//format files for read
	private function formatFile($file){
		return $this->baseDir.trim($file).$this->ext;
	}	
	
	
	
	
	private function makeSafe($data){
		return str_replace(Array("\n","\r",'|'),'',$data);
	}
	
	
	private function loadUserList(){
		if(sizeof($this->userListCache)>0){
			$usrList = $this->userListCache;
		}else{
			$users = $this->readAsArray('users');
			$usrList = Array();
			foreach($users as $user){
				$currentItem = explode('|',$user);
				/// username | id
				$usrList[$this->makeSafe($currentItem['0'])] = $this->makeSafe($currentItem[1]);
			}
			$this->userListCache = $usrList ;
		}
		return $usrList;
	}
	
	
	public function set_row($table,$getByColum,$useValue,$keyed_value_array){
		if($table !== 'users') die('throw error unsupported operation');
		if($getByColum !== 'uid') die('throw error unsupported operation');
		
		$writeString = ''; 
		foreach($keyed_value_array as $key => $value){
			$key = $this->makeSafe($key);
			$value = $this->makeSafe($value);
			$writeString .= "{$key}|{$value}\n";
		}
		$userFile = 'users'.'/'.$keyed_value_array['uid'];

		$this->write($userFile,$writeString);
		
		return true;
	}

	public function create_row($table,$values_array){
		// limits
		if($table !== 'users') die('throw error unsupported operation');

		$newUID = sizeof($this->loadUserList())+1;
		$values_array['uid'] = $newUID;
		$userdata = '';
		//proccess details
		foreach ($values_array as $key => $value){
			$key = $this->makeSafe($key);
			$value = $this->makeSafe($value);
			$userdata .= "{$key}|{$value} \n";
		}

		//create user file
		$userFile = 'users'.'/'.$newUID;
		$this->write($userFile,$userdata);
		//add to user list
		$this->appendWrite('users',$this->makeSafe($values_array['name']).'|'.$newUID."\n");
		//go gadget go
		return true;
	}
	
	

	
	//read file as array
	private function readAsArray($file) {
		$file = $this->formatFile($file);
		return file($file);
	}	
	//read file
	private function readFile($file) {
		$file = $this->formatFile($file);
		return file_get_contents($file);

	}
	//check file exists
	private function fileExist($file){

		$file = $this->formatFile($file);
		return file_exists($file);
	}
	//Write file
	private function write($file,$newData) {

		return file_put_contents($this->formatFile($file),$newData, LOCK_EX);
	}
	//Append write
	private function appendWrite($file,$dataToAdd) {
		return file_put_contents($this->formatFile($file),$dataToAdd, FILE_APPEND | LOCK_EX);
	}

}



?>